<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------


//------------------------
// 公共函数
//-------------------------


/**
 * 对象 转 数组
 *
 * @param object $obj 对象
 * @return array
 */
function object_to_array($obj) {
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }
 
    return $obj;
}


/**
 * 字节转换
 * @param $cate
 * @param $id
 * @return array
 */
function formatBytes($bytes) {

    if($bytes >= 1073741824) {
        $bytes = round($bytes / 1073741824 * 100) / 100 . ' GB';
    } elseif($bytes >= 1048576) {
        $bytes = round($bytes / 1048576 * 100) / 100 . ' MB';
    } elseif($bytes >= 1024) {
        $bytes = round($bytes / 1024 * 100) / 100 . ' KB';
    } else {
        $bytes = $bytes . 'Bytes';
    }

    return $bytes;

}


/**
 * 缩略图
 * @param $cate
 * @param $pid
 * @return array
 */

if (!function_exists('get_thumb')) {

	function get_thumb($src = '', $width = 480, $height = 320, $type = 3, $replace = false)
	{
		$src = substr($src,strpos($src,'storage/'));		
		$src = './' . $src;
		if (is_file($src) && file_exists($src)) {
			$ext = pathinfo($src, PATHINFO_EXTENSION);
			$name = basename($src, '.' . $ext);
			$dir = dirname($src);
			if (in_array($ext, array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) {
				$name = $name . '_thumb_' . $width . '_' . $height . '.' . $ext;
				$file = $dir . '/' . $name;
				if (!file_exists($file) || $replace == true) {
					$image = \think\Image::open($src);
					$image->thumb($width, $height, $type);
					$image->save($file);
				}
				$file = str_replace("\\", "/", $file);
				$file = '/' . trim($file, './');
				return $file;
			}
		}
		$src = str_replace("\\", "/", $src);
		$src = '/' . trim($src, './');
		return $src;
	}

}



/***
 * 获取当前栏目ID
 * @return mixed
 */
function get_cate_id()
{
    if (\think\facade\Request::has('name')) {
        $result = \app\common\model\Category::where('name', '=', \think\facade\Request::param('name'))->value('id');
    } else {
        $result = \app\common\model\Category::where('name', '=', \think\facade\Request::controller())->value('id');
    }
    return $result;
}



/**
 * 传递一个父级分类ID返回所有子分类ID
 * @param $cate
 * @param $pid
 * @return array
 */
function get_childs_id($cate, $pid)
{
    $arr = [];
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $arr[] = $v;
            $arr = array_merge($arr, get_childs_id($cate, $v['id']));
        }
    }
    return $arr;
}

/**
 * 格式化分类数组为字符串
 * @param $ids
 * @param string $pid
 * @return string
 */
function get_childs_id_str($ids, $pid = '')
{
    $result = '';
    foreach ($ids as $k => $v) {
        $result .= $v['id'] . ',';
    }
    if ($pid) {
        $result = $pid . ',' . $result;
    }
    $result = rtrim($result, ',');
    return $result;
}




/**
 * IP地址获取省市区
 * @param $ip
 * @return string
 */

function get_ip_address($ip){
    if(empty($ip)) return false;
    $url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
    $result = @file_get_contents($url);
    $result = json_decode($result,true);
    if($result['code']!==0 || !is_array($result['data'])) return false;
    return $result['data'];
}
